package com.resou.reader.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.resou.reader.R;
import com.resou.reader.api.entry.FilterBean;
import com.resou.reader.mine.vipcenter.callback.IFilterViewCallbackListener;
import com.resou.reader.utils.UIHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 筛选标签
 */
public class FilterView extends LinearLayout {
    LinearLayout llSubcategoryheader;
    AutoRadioGroup mAutoRadioGroup;
    AutoRadioGroup mAutoRadioGroup2;
    AutoRadioGroup mAutoRadioGroup3;
    View view;
    FilterBean mFilterBean;
    FilterBean mFilterBean2;
    FilterBean mFilterBean3;
    int cate = -1;
    String cateLabel = "";
    int word = -1;
    String wordLabel = "";
    int serial = -1;
    String serialLabel = "";
    public int mHeight = 10;

    public FilterView(Context context) {
        super(context);
        view = LayoutInflater.from(context).inflate(R.layout.filterview, this);
        initView();
    }

    public FilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        view = LayoutInflater.from(context).inflate(R.layout.filterview, this);
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        llSubcategoryheader = findViewById(R.id.llSubcategoryheader);
        mAutoRadioGroup = findViewById(R.id.mAutoRadioGroup);
        mAutoRadioGroup2 = findViewById(R.id.mAutoRadioGroup2);
        mAutoRadioGroup3 = findViewById(R.id.mAutoRadioGroup3);
        /**
         *获取 头部高度
         */
        llSubcategoryheader.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                llSubcategoryheader.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mHeight = llSubcategoryheader.getMeasuredHeight();
            }
        });
    }

    /**
     * 布局实例
     *
     * @return
     */
    public FilterView getFilterView() {
        return this;
    }

    public int getmHeight() {
        return mHeight;
    }


    /**
     * 设置分类标签数据
     *
     * @param filterBean
     * @return
     */
    public FilterView setFilterData(FilterBean filterBean) {
        this.mFilterBean = filterBean;
        setTypeData();
        return this;
    }

    /**
     * 设置分类标签数据
     *
     * @param filterBean
     * @return
     */
    public FilterView setFilterData2(FilterBean filterBean) {
        this.mFilterBean2 = filterBean;
        setFontData();
        return this;
    }

    /**
     * 设置分类标签数据
     *
     * @param filterBean
     * @return
     */
    public FilterView setFilterData3(FilterBean filterBean) {
        this.mFilterBean3 = filterBean;
        setRemindData();
        return this;
    }


    @SuppressLint("ResourceType")
    private void setTypeData() {
        List<FilterBean.FilterListBean> listBeans = (List<FilterBean.FilterListBean>) mFilterBean.getFilterList();
        listBeans.add(0, new FilterBean.FilterListBean(-1, "全部"));
        for (int i = 0; i < listBeans.size(); i++) {
            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setText(listBeans.get(i).getFilterDesc());
            radioButton.setButtonDrawable(null);
            radioButton.setTextColor(getResources().getColorStateList(R.drawable.font_check));
            radioButton.setGravity(Gravity.CENTER);
            if (i == 0) {
                radioButton.setTag(-1);
            } else {
                radioButton.setTag(listBeans.get(i).getFilterCode());
            }
            radioButton.setBackgroundResource(R.drawable.shap_check);
            radioButton.setTextSize(12);
            radioButton.setHeight(UIHelper.dip2px(getContext(), 25));
            radioButton.setPadding(UIHelper.dip2px(getContext(), 10), 3, UIHelper.dip2px(getContext(), 10), 3);
            //设置RadioButton间距margin
            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(5, UIHelper.dip2px(getContext(), 3), 5, UIHelper.dip2px(getContext(), 3));
            mAutoRadioGroup.addView(radioButton, params);
            if (i == 0) {
                mAutoRadioGroup.check(radioButton.getId());
                radioButton.setChecked(true);
            }
        }
        mAutoRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = findViewById(checkedId);
                radioButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cate = Integer.valueOf(radioButton.getTag().toString());
                        cateLabel = radioButton.getText().toString() + "";
                        if (iFilterViewCallbackListener != null) {
                            iFilterViewCallbackListener.setITypeCallbackListener(cate, cateLabel, word, wordLabel, serial, serialLabel);
                        }
                    }
                });
            }
        });
    }

    /**
     * 设置三级级菜单项数据
     */
    @SuppressLint("ResourceType")
    private void setFontData() {
        List<FilterBean.FilterListBean> listBeans = (List<FilterBean.FilterListBean>) mFilterBean2.getFilterList();
        listBeans.add(0, new FilterBean.FilterListBean(-1, "全部"));
        for (int i = 0; i < listBeans.size(); i++) {
            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setText(listBeans.get(i).getFilterDesc() + "");
            radioButton.setButtonDrawable(null);
            radioButton.setTextColor(getResources().getColorStateList(R.drawable.font_check));
            radioButton.setGravity(Gravity.CENTER);
            if (i == 0) {
                radioButton.setTag(-1);
            } else {
                radioButton.setTag(listBeans.get(i).getFilterCode());
            }
            radioButton.setBackgroundResource(R.drawable.shap_check);
            radioButton.setTextSize(12);
            radioButton.setHeight(UIHelper.dip2px(getContext(), 25));
            radioButton.setPadding(UIHelper.dip2px(getContext(), 10), 3, UIHelper.dip2px(getContext(), 10), 3);
            //设置RadioButton间距margin
            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(5, UIHelper.dip2px(getContext(), 3), 5, UIHelper.dip2px(getContext(), 3));
            mAutoRadioGroup2.addView(radioButton, params);
            if (i == 0) {
                mAutoRadioGroup2.check(radioButton.getId());
                radioButton.setChecked(true);
            }
        }
        mAutoRadioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = findViewById(checkedId);
                radioButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        wordLabel = (String) radioButton.getText();
                        word = Integer.valueOf(radioButton.getTag().toString());
                        if (iFilterViewCallbackListener != null) {
                            iFilterViewCallbackListener.setITypeCallbackListener(cate, cateLabel, word, wordLabel, serial, serialLabel);
                        }
                    }
                });
            }
        });
    }

    /**
     * 设置四级级菜单项数据
     */
    @SuppressLint("ResourceType")
    private void setRemindData() {
        List<FilterBean.FilterListBean> listBeans = (List<FilterBean.FilterListBean>) mFilterBean3.getFilterList();
        listBeans.add(0, new FilterBean.FilterListBean(-1, "全部"));

        for (int i = 0; i < listBeans.size(); i++) {
            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setText(listBeans.get(i).getFilterDesc());
            radioButton.setButtonDrawable(null);
            radioButton.setTextColor(getResources().getColorStateList(R.drawable.font_check));
            radioButton.setGravity(Gravity.CENTER);
            if (i == 0) {
                radioButton.setTag(-1);
            } else {
                radioButton.setTag(listBeans.get(i).getFilterCode());
            }
            radioButton.setBackgroundResource(R.drawable.shap_check);
            radioButton.setTextSize(12);
            radioButton.setHeight(UIHelper.dip2px(getContext(), 25));
            radioButton.setPadding(UIHelper.dip2px(getContext(), 10), 3, UIHelper.dip2px(getContext(), 10), 3);
            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(5, UIHelper.dip2px(getContext(), 3), 5, UIHelper.dip2px(getContext(), 3));
            mAutoRadioGroup3.addView(radioButton, params);
            if (i == 0) {
                mAutoRadioGroup3.check(radioButton.getId());
                radioButton.setChecked(true);
            }
        }
        mAutoRadioGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = findViewById(checkedId);
                radioButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        serialLabel = (String) radioButton.getText();
                        serial = Integer.valueOf(radioButton.getTag().toString());
                        if (iFilterViewCallbackListener != null) {
                            iFilterViewCallbackListener.setITypeCallbackListener(cate, cateLabel, word, wordLabel, serial, serialLabel);
                        }
                    }
                });
            }
        });
    }

    public IFilterViewCallbackListener iFilterViewCallbackListener;

    public void setiFilterViewCallbackListener(IFilterViewCallbackListener iFilterViewCallbackListener) {
        this.iFilterViewCallbackListener = iFilterViewCallbackListener;
    }
}
